<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:IPAuthFilter.php,2010/05/31 15:48:02 
 * 
 **************************************************************************/
 
 
 
/**
 * @file IPAuthFilter.php
 * @author huqingping
 * @date 2010/05/31 15:48:02
 * @version 1.0 
 * @brief 
 *  
 **/

class IPAuthFilter implements IFilter
{
	function execute(WebApp $app)
	{
		if ($app->request->isPrivate) {
			$ip = $app->request->clientip;
			if ($ip !== '127.0.0.1' &&
				//如果是内部ip则检查范围，否则直接不让通过
				!($this->isLocalIp($ip) && $this->inRange($ip))) {
					throw new Exception('hapn.ipauthfail');
				}
		}
	}

	private function isLocalIp($ip)
	{
		if (0==strncmp($ip, '10.', 3) || 
			0==strncmp($ip, '172.', 4) || 
			0==strncmp($ip, '127.', 4) || 
			0==strncmp($ip, '192.', 4) ) { 
				return true;
			}   
		return false;
	}

	private function inRange($ip)
	{
		$range = Conf::get('hapn.privateip');
		if (!$range) {
			return true;
		}
		foreach ($range as $key) {
			if ( 0==strncmp($key, $ip, strlen($key)) ) {
				return true;
			}
		}
		return false;
	}
}





/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
